<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class News_model extends MY_Model
{
    public function __construct() {
        parent::__construct();
    }
	/**
	 * 获取列表
	 *
	 * return array
	 */
	public function getNewsList($data, $per_page, $limit, $filds="*"){
       $where = " ";
        
        if(!empty($data['keyword'])){
            $where .= " AND content like '%{$data['keyword']}%' OR title like '%{$data['keyword']}%' OR transferfrom like '%{$data['keyword']}%'";
        }
       
        if($data['readonly'] == 1){
            $where .= " AND readonly = 1";
        }
        if($data['categoryid'] != 0){
            $where .= " AND categoryid = {$data['categoryid']}";
        }
        $orderby = " ORDER BY  addtime DESC";
        if($data['reviewcnt'])
        {
            $orderby = " ORDER BY reviewcnt DESC, addtime DESC";
        }
        if($data['region'] != 0){
            $where .= " AND (region like '%{$data['region']}%' OR region ='' OR region is null )";
        }
    	if($per_page){
            	$per_page = ($per_page-1)*$limit;
    	}	
        $limit = " limit {$per_page},{$limit}";

        $sql = "SELECT {$filds} FROM blues_news WHERE isDel<>1 and onlinestatus=1  {$where}  {$orderby} {$limit} ";

        $query = $this->db->query($sql);
        $result =$query->result_array();
        return $result;
    }
    public function getActivityList($data, $per_page, $limit, $filds="*"){
        $where = " ";
        if($data['region'] != 0){
            $where .= " AND (region like '%{$data['region']}%' OR region ='' OR region is null )";
        }
        if($per_page){
            $per_page = ($per_page-1)*$limit;
        }   
        $limit = " limit {$per_page},{$limit}";
		$cur_time = time();
        $sql = "SELECT {$filds} FROM blues_news n,blues_question ques WHERE n.newsID=ques.newsID AND n.isDel<>1 and n.activity_endtime >= {$cur_time}  and n.onlinestatus=1  {$where} AND n.ispublish=1 GROUP BY n.newsID ORDER BY n.addtime DESC  {$limit} ";

        $query = $this->db->query($sql);
        $result =$query->result_array();
        return $result;
    }
    
    public function getMyActivityList($data, $per_page, $limit, $filds="*"){
        $where = " ";
        if($data['region'] != 0){
            $where .= " AND n.region like '%{$data['region']}%'";
        }
        if($data['AccountID']){
            $where .= " AND ans.AccountID = '{$data['AccountID']}'";
        }
        if($per_page){
            $per_page = ($per_page-1)*$limit;
        }   
        $limit = " limit {$per_page},{$limit}";

        $sql = "SELECT {$filds} FROM blues_news n,blues_question ques,blues_question_answer ans WHERE n.newsID=ques.newsID AND ques.newsID=ans.newsID AND n.isDel<>1 and n.onlinestatus=1 AND ans.isDel=0  {$where} GROUP BY n.newsID  ORDER BY n.addtime DESC {$limit} ";

        $query = $this->db->query($sql);
        $result =$query->result_array();
        return $result;
    }

    //根据评论id，获取评论详情
    public function getRiviewInfo($newsID){
        $sql = "SELECT 
                    r.reviewId,r.content,r.createtime,r.fromUsername ,u.nickname,u.avatar
                FROM 
                    blues_news_review r 
                , 
                    blues_user u 
                WHERE 
                    u.username=r.fromUsername 
                AND 
                    r.newsID={$newsID} 
                AND 
                    r.type=1 
                AND 
                    r.status =1
                ORDER BY 
                    r.createtime DESC";
        $query = $this->db->query($sql);
        $result = $query->result_array();
        return $result;
    }



     //发布动态
    public function publishNewsReview($data = array(),$picList){
        $res = $this->db->insert("blues_news_review",$data);
        return $res;
    }

    /**
     * 根据id获取新闻评论数
     * @return [type] [description]
     */
    public function getNewsReviewCnt($newsID){
    	$sql = "SELECT count(*) cnt FROM blues_news_review WHERE  status=1  and newsID = {$newsID}";

        $query = $this->db->query($sql);
        $result =$query->row_array();
        return $result['cnt'];
    }

    //获取动态说说详情
    public function getNewsDetail($newsID,$filds = "*"){
        $sql = "SELECT 
                    {$filds} 
                FROM 
                    blues_news p 
                WHERE 
                    newsID={$newsID} 
                AND 
                    isDel = 0 ";
        $query = $this->db->query($sql);
        $result =$query->row_array();
        //判断该用户是否收藏过该评论
        
        return $result;
    }

//根据id获取评论
    public function getReviewList($newsID){
    	$sql = "SELECT u.AccountID,r.content,r.createtime,u.nickname,u.avatar,r.reviewId FROM blues_news_review r ,blues_user u WHERE r.fromUsername=u.username AND r.status=1 AND r.newsID = {$newsID} ORDER BY r.createtime DESC";

        $query = $this->db->query($sql);
        $result =$query->result_array();
        return $result;
    }


     //判断新闻是否被收藏过
    public function checkSetCollected($data = array()){
        //检查是否已存在
	$sql = "SELECT * FROM blues_news_collect where newsID={$data['newsID']} and fromUsername='".$data['fromUsername']."'";
	$query = $this->db->query($sql);
        $res = $query->row_array();
        return $res;
    }

    //添加收藏
    public function setCollect($data = array(),$type = 0){
        //检查是否已存在
        $this->db->where(array("newsID"=>$data['newsID'],"fromUsername"=>$data['fromUsername']));
        $query = $this->db->get("blues_news_collect");
        $collect_result = $query->row_array();
        if($type === 0 && $collect_result){
            //取消收藏
            $this->db->where("id",$collect_result['id']);
            $res = $this->db->delete("blues_news_collect");
        }else{
            //收藏
            if($collect_result){
                $res = 70001;
            }else{
                $res = $this->db->insert("blues_news_collect",$data);
            }
            
        }
        return $res;
    }
    //添加评论点赞
    public function setReviewPraise($data = array(),$type = 0){
        //检查是否已存在
        $this->db->where(array("reviewid"=>$data['reviewid'],"fromUsername"=>$data['fromUsername']));
        $query = $this->db->get("blues_news_review_praise");
        $praise_result = $query->row_array();
        if($type === 0 && $praise_result){
            //取消收藏
            $this->db->where("id",$praise_result['id']);
            $res = $this->db->delete("blues_news_review_praise");
        }else{
            //收藏
            if($praise_result){
                $res = 70003;
            }else{
                $res = $this->db->insert("blues_news_review_praise",$data);
            }
        }
        return $res;
    }

    //添加文章点赞
    public function setPraise($data = array(),$type = 0){
        //检查是否已存在
        $this->db->where(array("newsID"=>$data['newsID'],"fromUsername"=>$data['fromUsername']));
        $query = $this->db->get("blues_news_praise");
        $praise_result = $query->row_array();
        if($type === 0 && $praise_result){
            //取消收藏
            $this->db->where("id",$praise_result['id']);
            $res = $this->db->delete("blues_news_praise");
        }else{
            //收藏
            if($praise_result){
                $res = 70003;
            }else{
                $res = $this->db->insert("blues_news_praise",$data);
            }
        }
        return $res;
    }

    public function hasSetPraise($newsID,$fromUsername){
        //检查是否已存在
        $this->db->where(array("newsID"=>$newsID,"fromUsername"=>$fromUsername));
        $query = $this->db->get("blues_news_praise");
        $praise_result = $query->num_rows();
        return $praise_result?$praise_result:0;
    }

    public function hasSetReviewPraise($reviewid,$fromUsername){
        //检查是否已存在
        $this->db->where(array("reviewid"=>$reviewid,"fromUsername"=>$fromUsername));
        $query = $this->db->get("blues_news_review_praise");
        $praise_result = $query->num_rows();
        return $praise_result?$praise_result:0;
    }

    public function getPraiseCnt($newsID){
        $this->db->where(array("newsID"=>$newsID));
        $query = $this->db->get("blues_news_praise");
        return  $query->num_rows()?$query->num_rows():0;
    }
    public function getReviewPraiseCnt($reviewid){
        $this->db->where(array("reviewid"=>$reviewid));
        $query = $this->db->get("blues_news_review_praise");
        return  $query->num_rows()?$query->num_rows():0;
    }

    public function newsUpdate($newsID = 0)
    {
        $sql = "UPDATE blues_news SET count = count+1 WHERE newsID = {$newsID}";
        $res = $this->db->query($sql);
        return $res;
    }

}

